﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.Services;
using DDSC.OracleADO;
using System.Collections;
using System.Text;
using System.Data;
using System.Web.UI.HtmlControls;

public partial class bas_category :  BasePermissionPage
{
    public class clsMIS_CODE_DETAIL
    {
        public string Dept_ID { get; set; }
        public string Document { get; set; }
        public string Document_NAME{ get; set; }
        public string DATA_SEQ { get; set; }
        public string STATUS { get; set; }
        public string USER { get; set; }
        
        

    }
    //Grid選取資料
    public static Dictionary<string, string> GridKey { get; set; }
    //查詢條件
    public static Dictionary<string, string> ConditionKey { get; set; }
    //紀錄選取資料及查詢條件
    [WebMethod]
    public static void SetGridKey(Dictionary<string, string> p_Param, Dictionary<string, string> p_ConditionKey)
    {
        ConditionKey = p_ConditionKey;
        GridKey = p_Param;
    }
    [WebMethod]
    public static Object CATEGORY(string DEPT_ID, string CATEGORY_ID)
    {
        dbAccess m_dba = new dbAccess();
        Object results = new Object();
        StringBuilder m_sb = new StringBuilder();

        try
        {
            DataSet ds = null;
            ArrayList m_arrParam = new ArrayList();
            m_sb.Clear();
            m_sb.Append(" select  dept_id, document_category, document_category_name, document_no, document_name, remark, data_seq, status ");
            m_sb.Append(" FROM KRJ_CATEGORY_DOCUMENT A  ");
            m_sb.Append(" Where dept_id = :DEPT_ID ");
            m_arrParam.Add(m_dba.GetOracleParam(":DEPT_ID", DEPT_ID));
            if (CATEGORY_ID != "")
            {
                m_sb.Append(" AND document_category = :CATEGORY_ID ");
                m_arrParam.Add(m_dba.GetOracleParam(":CATEGORY_ID", CATEGORY_ID));
            }
            m_sb.Append(" order by dept_id , data_seq ");
            ds = m_dba.runExecuteQuery("CATEGORY", m_sb.ToString(), m_arrParam);

            if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "000" )
            {
                results = m_dba.RowsToDictionary(ds.Tables["CATEGORY"], ds.Tables["CATEGORY"].Rows.Count.ToString(), "1");
                //m_dba.ToJson(ds,"1","1"
            }
            else
            {
                if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "100")
                {
                    results = "error_no";
                }
                else
                {
                    results = "error資料讀取錯誤！" + ds.Tables[0].Rows[0]["LogMessage"];
                }
            }
        }
        catch (Exception ex)
        {
            results = "error" + ex.Message;
        }
        return results;
    }
    [WebMethod]
    public static Object getCATEGORY(string DEPT_ID, string CATEGORY_ID)
    {
        dbAccess m_dba = new dbAccess();
        Object results = new Object();
        StringBuilder m_sb = new StringBuilder();

        try
        {
            DataSet ds = null;
            ArrayList m_arrParam = new ArrayList();
            m_sb.Clear();
            m_sb.Append(" select  distinct document_category, document_category_name ");
            m_sb.Append(" FROM KRJ_CATEGORY_DOCUMENT A  "); 
            m_sb.Append(" Where dept_id = :DEPT_ID ");
            m_arrParam.Add(m_dba.GetOracleParam(":DEPT_ID", DEPT_ID));
            if (CATEGORY_ID != "")
            {
                m_sb.Append(" and document_category = :CATEGORY_ID ");
                m_arrParam.Add(m_dba.GetOracleParam(":CATEGORY_ID", CATEGORY_ID));
            }
            m_sb.Append(" order by document_category ");
            ds = m_dba.runExecuteQuery("CATEGORY", m_sb.ToString(), m_arrParam);

            if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "000")
            {
                results = ds.Tables["CATEGORY"].Rows[0]["document_category_name"].ToString();
                //m_dba.ToJson(ds,"1","1"
            }
            else
            {
                if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "100")
                {
                    results = "error_no";
                }
                else
                {
                    results = "error資料讀取錯誤！" + ds.Tables[0].Rows[0]["LogMessage"];
                }
            }
        }
        catch (Exception ex)
        {
            results = "error" + ex.Message;
        }
        return results;
    }
    [WebMethod]
    public static Object qtyCATEGORY(string DEPT_ID, PageInfo p_PageInfo)
    {
        dbAccess m_dba = new dbAccess();
        Object results = new Object();
        StringBuilder m_sb = new StringBuilder();

        try
        {
            DataSet ds = null;
            ArrayList m_arrParam = new ArrayList();
            m_sb.Clear();
            m_sb.Append(" select distinct A.dept_id || ' ' || B.dept_name dept_id, A.document_category category_id, A.document_category_name ");
            m_sb.Append(" FROM KRJ_CATEGORY_DOCUMENT A left join mis_dept_ap B on A.DEPT_ID = B.DEPT_ID  ");
            m_sb.Append(" Where 1=1 ");
            if (DEPT_ID != "")
            {
                m_sb.Append(" and A.dept_id = :DEPT_ID ");
                m_arrParam.Add(m_dba.GetOracleParam(":DEPT_ID", DEPT_ID));
            }

            results = ADOGridHandler(m_sb, m_arrParam, p_PageInfo);
        }
        catch (Exception ex)
        {
            results = "error" + ex.Message;
        }
        return results;
    }
    [WebMethod]
    public static Object Maintain_Save(string Dept_ID, string Category, string Category_NAME, List<clsMIS_CODE_DETAIL> DetlCtrls)
    {
        Object results = new Object();
        try
        {
            ddscDataSet m_ddscDs = new ddscDataSet();
            dbAccess m_dba = new dbAccess();
            ddscDataTable p_ddscDataTable = m_ddscDs.createDataTable("KRJ_CATEGORY_DOCUMENT");
            DataBase dataBase = new DataBase();
            // 宣告回傳DataSet
            DataSet dsResult = new DataSet();
            //先刪除
        
            int i = 0;
            foreach (clsMIS_CODE_DETAIL m_clsMIS_CODE_DETAIL in DetlCtrls)
            {
                if (i == 0)
                {
                    p_ddscDataTable.addNewRow();
                    p_ddscDataTable.setColumnValue("action", "NVARCHAR", "DEL");
                    p_ddscDataTable.setColumnValue("dept_id", "NVARCHAR", Dept_ID);
                    p_ddscDataTable.setWhereValue("dept_id", Dept_ID);
                    if (Category != "")
                    {
                        p_ddscDataTable.setColumnValue("document_category", "NVARCHAR", Category);
                        p_ddscDataTable.setWhereValue("document_category", Category);
                    }
                }
                if (m_clsMIS_CODE_DETAIL.Document.Trim() != "")
                {
                    p_ddscDataTable.addNewRow();
                    p_ddscDataTable.setColumnValue("action", "NVARCHAR", "ADD");
                    p_ddscDataTable.setColumnValue("dept_id", "NVARCHAR", m_clsMIS_CODE_DETAIL.Dept_ID);
                    p_ddscDataTable.setColumnValue("document_category", "NVARCHAR", Category);
                    p_ddscDataTable.setColumnValue("document_category_name", "NVARCHAR", Category_NAME);
                    p_ddscDataTable.setColumnValue("document_no", "NVARCHAR", m_clsMIS_CODE_DETAIL.Document);
                    p_ddscDataTable.setColumnValue("document_name", "NVARCHAR", m_clsMIS_CODE_DETAIL.Document_NAME);
                    p_ddscDataTable.setColumnValue("remark", "NVARCHAR", "");
                    p_ddscDataTable.setColumnValue("data_seq", "NUMBER", m_clsMIS_CODE_DETAIL.DATA_SEQ);
                    p_ddscDataTable.setColumnValue("status", "VARCHAR", m_clsMIS_CODE_DETAIL.STATUS);

                    p_ddscDataTable.setColumnValue("CRT_ID", "NVARCHAR", m_clsMIS_CODE_DETAIL.USER);
                    p_ddscDataTable.setColumnValue("CRT_DATE", "NVARCHAR", System.DateTime.Now.ToString("yyyyMMdd"));
                    p_ddscDataTable.setColumnValue("CRT_TIME", "NVARCHAR", System.DateTime.Now.ToString("HHmmss"));

                    p_ddscDataTable.setColumnValue("UPD_ID", "NVARCHAR", m_clsMIS_CODE_DETAIL.USER);
                    p_ddscDataTable.setColumnValue("UPD_DATE", "NVARCHAR", System.DateTime.Now.ToString("yyyyMMdd"));
                    p_ddscDataTable.setColumnValue("UPD_TIME", "NVARCHAR", System.DateTime.Now.ToString("HHmmss"));
                }
                else
                {
                    results = "1!"; 
                }
                i = i + 1;
            }

            if (p_ddscDataTable.Rows.Count > 1)
            {
                m_ddscDs.addddscDataTable(p_ddscDataTable);
            }
            // 執行編輯method
            DataBase m_DataBase = new DataBase();
            dsResult = m_DataBase.AdditionParameter(m_ddscDs);
            string strResult = dsResult.Tables[0].Rows[0]["TransResult"].ToString(); 
            string strMessage = dsResult.Tables[0].Rows[0]["Message"].ToString();
            string strLogMessage = dsResult.Tables[0].Rows[0]["LogMessage"].ToString();
            if (strResult == "T")
            {
                results = "存檔成功!"; 
            }
            else
            {
                results = "error" + strMessage;
            }
        }
        catch (Exception ex)
        {
            results = "error" + ex.Message;
        }
        return (results);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            DataSet ds = null;
            ds = DBINT();
            //DropDownList資料bind入
            ddldept.DataTextField = "DEPT_NAME";
            ddldept.DataValueField = "DEPT_ID";
            ddldept.DataSource = ds.Tables["DEPT"];
            ddldept.DataBind();

            hiUSER_ID.Value = User.Identity.Name;
            if (!Page.IsPostBack)
            {
                //新增一筆明細資料
                //for (Int32 i = 0; i < 15; i++)
                //{
                //    AddDetailRow("", "", "", "");
                //}

            }
            //ClientScript.RegisterClientScriptBlock(this.GetType(), "btnSearch_Click", "<script language='javascript'>$(document).ready(function () {Add_NEW_ROW(); }) </script>");
        }
        catch (Exception ex)
        {
            //Response.Write(e.Message);
            lbl_message.Text = ex.Message;

        }
    }
    protected DataSet DBINT()
    {
        DataSet ds = null;
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();
            m_sb.Append(" select A.bi4101 DEPT_ID ,A.bi4101 || '-' || B.DEPT_NAME DEPT_NAME ");
            m_sb.Append(" from as400_bif41 A left join MIS_DEPT_AP  B on A.BI4101 = B.DEPT_ID ");
            m_sb.Append(" where A.bi4101 < '1000' ");
            m_sb.Append(" order by A.bi4101 ");
            ds = m_dba.runExecuteQuery("DEPT", m_sb.ToString(), m_arrParam);
            if (ds.Tables[0].Rows[0]["ReturnCode"] != "000")
            {
                lbl_message.Text = "資料讀取錯誤！" + ds.Tables[0].Rows[0]["LogMessage"];
            }

        }
        catch (Exception ex)
        {
            lbl_message.Text = ex.Message;
        }
        return ds;
    }
    public void AddDetailRow(string txtCategory, string txtCategory_NAME, string txtDocument, string txtDocument_NAME)
    {
        HtmlTableRow tr = new HtmlTableRow();
        HtmlTableCell td = default(HtmlTableCell);
        String[] m_GridWidth = DetailTitle.Attributes["GridWidth"].Split(',');
        TextBox TB = new TextBox();
        int j = 0;
        
        try
        {
            while (j < 5)
            {
                td = new HtmlTableCell();
                TB = new TextBox();
                switch (j)
                {
                    case 0:
                        //刪除圖片
                        
                        td.Style.Add("WIDTH", m_GridWidth[j]);
                        ImageButton Img = new ImageButton();
                        Img.ID = "IMG_Del_0_" + DetailTable.Rows.Count;
                        
                        Img.ImageUrl = "../../Images/Desc.png";
                        Img.OnClientClick = "DeleteRow(event,this.id)";
                        td.Controls.Add(Img);
                        tr.Cells.Add(td);
                        break;
                    case 1:
                        TB.ID = "txtCategory";
                        TB.Text = "";
                        TB.Attributes.Add("style", "width:90%");
                        td.Style.Add("WIDTH", m_GridWidth[j]);
                        td.Controls.Add(TB);

                        tr.Cells.Add(td);
                        break;
                    case 2:
                        TB.ID = "txtCategory_NAME";
                        TB.Attributes.Add("style", "width:90%");
                        td.Style.Add("WIDTH", m_GridWidth[j]);
                        td.Controls.Add(TB);

                        tr.Cells.Add(td);
                        break; 
                    case 3:
                        TB.ID = "txtDocument";
                        TB.Attributes.Add("style", "width:90%");
                        td.Style.Add("WIDTH", m_GridWidth[j]);
                        td.Controls.Add(TB);

                        tr.Cells.Add(td);
                    break; 
                    case 4:
                        TB.ID = "txtDocument_NAME";
                        TB.Attributes.Add("style", "width:90%");
                        td.Style.Add("WIDTH", m_GridWidth[j]);
                        td.Controls.Add(TB);

                        tr.Cells.Add(td);
                    break; 
                }
                j += 1;
            }
            DetailTable.Rows.Add(tr);
        }
        catch (Exception ex)
        {
            lbl_message.Text=ex.Message;
        }
    }
    protected void btn_exist_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
}